package com.cysyz.commonservice.mycommonservice.impl;


import java.util.Date;

import com.cysyz.commonservice.mycommonservice.BaseService;
import com.cysyz.commonservice.mycommonservice.ResetPasswordByEmailService;
import com.cysyz.commonservice.mycommonservice.exception.InputValueIsNullOrEmptyException;
import com.cysyz.commonservice.mycommonservice.exception.MD5NotMatchException;
import com.cysyz.commonservice.mycommonservice.exception.UrlExpireException;
import com.cysyz.commonservice.mycommonservice.exception.ValueNotMatchException;
import com.cysyz.commonservice.mycommonservice.support.ResetPasswordByEmailAble;
import com.cysyz.utils.MD5;
import com.cysyz.utils.SMTPMailSender;

public class ResetPasswordByEmailServiceImpl extends BaseService implements ResetPasswordByEmailService{
	/* (non-Javadoc)
	 * @see com.cysyz.mycommonservice.impl.ResetPasswordByEmailService#resetPasswordByEmail(com.cysyz.mycommonservice.ResetPasswordByEmailAble, java.lang.String)
	 */
	@Override
	public void resetPasswordByEmail(ResetPasswordByEmailAble input,  String newPassword) throws InputValueIsNullOrEmptyException, MD5NotMatchException, UrlExpireException, ValueNotMatchException, InvalidIdException {
		checkNullOrEmpty(input, "id","mail");
		checkNullOrEmpty(newPassword, "password is empty");
		
		ResetPasswordByEmailAble dbEntity = dao.get(input.getClass(), input.getId());
		
		checkValueEqual(input, dbEntity, "id", "password");
		if (dbEntity.getUrlExpireTime().getTime() < System.currentTimeMillis()) {
			throw new UrlExpireException("the url is expire");
		}
		
		try {
			checkValueEqual(input, dbEntity, "resetPsMD5");
		} catch (ValueNotMatchException e) {
			throw new MD5NotMatchException("md5 not match");
		}
		dbEntity.setPassword(MD5.MD5Encode(newPassword));
	}
	
	/* (non-Javadoc)
	 * @see com.cysyz.mycommonservice.impl.ResetPasswordByEmailService#generateResetPsUrl(com.cysyz.mycommonservice.ResetPasswordByEmailAble, java.lang.String)
	 */
	@Override
	public void generateResetPsUrl(ResetPasswordByEmailAble input, String urlPrefix, MailContentGenerator mailContentGenerator) throws InputValueIsNullOrEmptyException, ValueNotMatchException, InvalidIdException {
		checkNullOrEmpty(input, "id", "password");
		ResetPasswordByEmailAble dbEntity = dao.get(input.getClass(), input.getId().trim());
		checkValueEqual(input, dbEntity, "id", "password");
		long currentTimeMillis = System.currentTimeMillis();
		String md5 = generateResetPsMD5(dbEntity.getId(), currentTimeMillis);

		StringBuilder url = generateUrl(urlPrefix, dbEntity, md5);
		
		dbEntity.setResetPsMD5(md5);
		dbEntity.setUrlExpireTime(new Date(currentTimeMillis + 20 * 60 * 60 * 1000));
		
		SMTPMailSender mailSender = new SMTPMailSender("smtp.163.com", "cysyz1229", "sunyingzi198572");
		try {
			mailSender.generateAMail("崔勇:<cysyz1229@163.com>", dbEntity.getMail(), "密码修改", mailContentGenerator.generateMailContent(url)).send();
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}

	private String generateResetPsMD5(String id, long currentTimeMillis) {
		StringBuilder md5 = new StringBuilder(id);
		md5.append(currentTimeMillis);
		return MD5.MD5Encode(md5.toString());
	}
	
	private StringBuilder generateUrl(String urlPrefix,
			ResetPasswordByEmailAble dbEntity, String md5) {
		StringBuilder url = new StringBuilder(urlPrefix);
		url.append("?");
		url.append("id=").append(dbEntity.getId()).append("&");
		url.append("uuid=").append(md5);
		return url;
	}

}
